import config from "./config.js";
const {BASEURL} = config

const request = ({ url = '', param = {}, loading = true, ...other } = {}) => {
    return new Promise((resolve, reject) => {
		if (loading) {
			uni.showLoading({
			    title: '请求中...',
			    mask: other.load || false // 默认遮罩出现可以继续操作
			});
		}
        try{
			/*const p2 = {
			  code: CODE,
			  param: encrypt(JSON.stringify(param), CODE, SIGN)
			}*/
            uni.request({
                url: getUrl(BASEURL,url),
                data: param, //请求超时在manifest.json配置
				...other,
                header: {
                    'content-type':'application/json',
					'authorization': uni.getStorageSync('authorization') || ''
                },
                success: res => {
					if (res.data.code == 200) {
						resolve(res.data)
						if (!other.noHideloading) {
							uni.hideLoading();
						}
					} else {
						if(res.data.code == 4001 || res.data.code == 4010 || res.data.code == 4020) {
							let routes = getCurrentPages(); // 获取当前打开过的页面路由数组
							let curRoute = '/' + routes[routes.length - 1].route
							setTimeout(() => {
								if (res.data.code == 4001 ) {
									uni.removeStorageSync('authorization')
									if(param.noRedirectLogin != '1') {
										uni.redirectTo({
											url: `/pages/login/index?rurl=${encodeURIComponent(curRoute)}`
										});
									}
								} else if (res.data.code == 4010) {
									uni.redirectTo({
										url: `/pages/openWz-step1/index`
									});
								} else if(res.data.code == 4020) {
									uni.redirectTo({
										url: `/pages/companyAuth/index`
									});
								}
							}, 2000)
						}
						if(res.data.code != 220002 && res.data.code != 220001) {
							uni.showToast({
							    title: res.data.msg,
							    icon: 'none'
							})
						}
						
						reject(res.data)
						if (!other.noHideloading) {
							setTimeout(() => {
								uni.hideLoading();
							}, 2000)
						}
						
					}
                },
                fail: (err) => {
                    reject(err.data);
                    console.log(err);
                    uni.showToast({
                        title: '请检查网络连接',
                        icon: 'none'
                    })
					setTimeout(() => {
						uni.hideLoading();
					}, 2000)
                    
                },
                complete: () => {
                    // uni.hideLoading();
                }
            });
        }catch(e){
            uni.hideLoading();
            uni.showToast({
                title: '服务端异常',
                icon: 'none'
            })
        }
        
    })
}


/**
 * 获取连接，以便别的域名的接口
 */
const getUrl = ((baseUrl,url) => {
  if (url.indexOf('://') > -1) {
	  return url
  } else {
	  return baseUrl + url
  }
  
  return encrypted.toString();
})



/**
 * url 接口地址
 * param 请求参数
 * loading 是否需要loading
 * isGx 是公信的接口还是企信易的接口
 * mask loading是否需要不可见的蒙版层避免点击
 */
const post = (url, param = {}, loading, mask, noHideloading) => {
  return request({
    url,
    param,
    loading,
	noHideloading,
    method: 'post',
    mask
  })
}

/**
 * url 接口地址
 * param 请求参数
 * loading 是否需要loading
 * isGx 是公信的接口还是企信易的接口
 * mask loading是否需要不可见的蒙版层避免点击
 */
const get = (url, param = {}, loading, mask, noHideloading) => {
  return request({
    url,
    param,
    loading,
	noHideloading,
    method: 'get',
    mask
  })
}
function debounce(fn, delay) {
    let timer = null
    return function () {
        let arg = arguments        
        clearTimeout(timer)      
        timer = setTimeout(() => {
            fn.apply(this, arg)
        }, delay)
    }
}


export default {
	post,
	get,
	debounce
}
